package com.lelefly.base.daoimp;

import java.util.Collection;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import com.lelefly.Log.Logger;
import com.lelefly.constant.Constant;
import com.lelefly.db.dbmanager.HibernateSessionFactory;

@Repository
public class BaseDao implements Logger {

	public int saveObject(Object model) throws Exception {
		int state = Constant.DB_STATUS_FAIL;

		SessionFactory sessionFactory = HibernateSessionFactory
				.getSessionFactory();

		Session session = sessionFactory.openSession();

		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.save(model);
			transaction.commit();

			state = Constant.DB_STATUS_SUCCESS;
		} catch (Exception e) {
			// TODO: handle exception

			getLogger().info("----->>>" + e.getMessage());
			transaction.rollback();
			throw e;
		} finally {
			session.close();
		}

		return state;
	}

	public int saveObjects(Collection objects) throws Exception {
		int state = Constant.DB_STATUS_FAIL;

		SessionFactory sessionFactory = HibernateSessionFactory
				.getSessionFactory();

		Session session = sessionFactory.openSession();

		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			for (Object object : objects) {
				session.save(object);
				session.flush();
				session.clear();
			}
			transaction.commit();
			state = Constant.DB_STATUS_SUCCESS;
		} catch (Exception e) {
			// TODO: handle exception
			getLogger().info("----->>>" + e.getMessage());
			transaction.rollback();
			throw e;
		} finally {
			session.close();
		}

		return state;
	}

	public int saveOrUpdateObject(Object model) throws Exception {
		int state = Constant.DB_STATUS_FAIL;

		SessionFactory sessionFactory = HibernateSessionFactory
				.getSessionFactory();

		Session session = sessionFactory.openSession();

		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.saveOrUpdate(model);
			transaction.commit();

			state = Constant.DB_STATUS_SUCCESS;
		} catch (Exception e) {
			// TODO: handle exception
			getLogger().info("----->>>" + e.getMessage());
			transaction.rollback();
			throw e;
		} finally {
			session.close();
		}

		return state;
	}

	public int UpdateObject(Object model) {
		int state = Constant.DB_STATUS_FAIL;
		SessionFactory sessionFactory = HibernateSessionFactory
				.getSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.update(model);
			transaction.commit();
			state = Constant.DB_STATUS_SUCCESS;
		} catch (Exception e) {
			// TODO: handle exception
			getLogger().info("----->>>" + e.getMessage());
			transaction.rollback();
			throw e;
		} finally {
			session.close();
		}
		return state;
	}

	public int deteleObjectbySql(String sql) {
		int state = Constant.DB_STATUS_FAIL;
		SessionFactory sessionFactory = HibernateSessionFactory
				.getSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			Query query = session.createQuery(sql);
			query.executeUpdate();
			transaction.commit();
			state = Constant.DB_STATUS_SUCCESS;
		} catch (Exception e) {
			// TODO: handle exception
			getLogger().info("----->>>" + e.getMessage());
			transaction.rollback();
			throw e;
		} finally {
			session.close();
		}
		return state;
	}

	@SuppressWarnings("rawtypes")
	public List queryObjetReList(String sql) {
		SessionFactory sessionFactory = HibernateSessionFactory
				.getSessionFactory();
		Session session = sessionFactory.openSession();
		try {
			Query query = session.createQuery(sql);
			return query.list();
		} catch (Exception e) {
			// TODO: handle exception
			getLogger().info("----->>>" + e.getMessage());
			throw e;
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("rawtypes")
	public List queryObjetReListBySql(String sql, Class model) {
		SessionFactory sessionFactory = HibernateSessionFactory
				.getSessionFactory();
		Session session = sessionFactory.openSession();
		try {
			Query query = session.createSQLQuery(sql).addEntity(model);
			return query.list();
		} catch (Exception e) {
			// TODO: handle exception
			getLogger().info("----->>>" + e.getMessage());
			throw e;
		} finally {
			session.close();
		}
	}
	
	public int updateObjectBySql(String sql){
		int state = Constant.DB_STATUS_FAIL;
		SessionFactory sessionFactory = HibernateSessionFactory
				.getSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			Query query = session.createQuery(sql);
			query.executeUpdate();
			transaction.commit();
			state = Constant.DB_STATUS_SUCCESS;
		} catch (Exception e) {
			// TODO: handle exception
			getLogger().info("----->>>" + e.getMessage());
			transaction.rollback();
			throw e;
		} finally {
			session.close();
		}
		return state;
	}

	public Object getObjcetReUnique(String hql) {

		SessionFactory sessionFactory = HibernateSessionFactory
				.getSessionFactory();
		Session session = sessionFactory.openSession();
		try {
			Query query = session.createQuery(hql);
			return query.uniqueResult();
		} catch (Exception e) {
			// TODO: handle exception
			getLogger().info("----->>>" + e.getMessage());
			throw e;
		} finally {
			session.close();
		}

	}

	public Object getObjcetReUniqueBySql(String sql) {

		SessionFactory sessionFactory = HibernateSessionFactory
				.getSessionFactory();
		Session session = sessionFactory.openSession();
		try {
			Query query = session.createSQLQuery(sql);
			return query.uniqueResult();
		} catch (Exception e) {
			// TODO: handle exception
			getLogger().info("----->>>" + e.getMessage());
			throw e;
		} finally {
			session.close();
		}

	}

	@Override
	public org.apache.log4j.Logger getLogger() {
		// TODO Auto-generated method stub
		return org.apache.log4j.Logger.getLogger(BaseDao.class);
	}
}
